package home;


import includes.MD5;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;

import member.MemberHome;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import president.PresidentHome;
import secretary.SecretaryHome;
import treasurer.TreasurerHome;

import administrator.AdministratorHome;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Account;
import controllers.User;


public class HomePage extends WebPage {
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model userModel = new Model();
	private Model passwdModel = new Model();
	MD5 md5 = new MD5 (); 

	public HomePage()throws IOException, SQLException {
	
	TextField id = new TextField("studentid", userModel);
	id.setRequired(true);
	
	TextField pass = new PasswordTextField("password", passwdModel);
	pass.setRequired(true);

	Form form = new Form("loginForm"){
	
	private static final long serialVersionUID = 1L;
	
	protected void onSubmit(){
		try {
			String studentid = (String) userModel.getObject();
			String password = (String) passwdModel.getObject();
			String newPassword = md5.md5(password);
			//System.out.println("student Id" + studentid);
			
			//System.out.println("new Password:" + md5.md5("jenn"));
			
			//see if account exists
			Account account = (Account)sqlMap.queryForObject("Account.getAccount", studentid);
			
			
			//if accounts exists			
			if(account!=null && newPassword.equals(account.getPassword()) ){			
				
				
				//check account type
				if((Integer.toString(account.getUserid())).equals("1")){						
					
					//set session to current user
					((Sessions)getSession()).setUserid(account.getUserid());
					((Sessions)getSession()).setStudentid(studentid);
					
					//search for user description 
					User user = (User)sqlMap.queryForObject("User.getUserdesc", account.getUserid() );
					
					//set current user description
					((Sessions)getSession()).setDesc(user.getUserdesc(account.getUserid()));	
					
					PresidentHome page = new PresidentHome();
					setResponsePage(page);
				}
				else if ((Integer.toString(account.getUserid())).equals("2"))
				{
					//set session to current user
					((Sessions)getSession()).setUserid(account.getUserid());
					((Sessions)getSession()).setStudentid(studentid);
					
					//search for user description 
					User user = (User)sqlMap.queryForObject("User.getUserdesc", account.getUserid() );
					
					//set current user description
					((Sessions)getSession()).setDesc(user.getUserdesc(account.getUserid()));
					
					
					SecretaryHome page = new SecretaryHome();
					setResponsePage(page);
					
				}
				else if ((Integer.toString(account.getUserid())).equals("3"))
				{
					//set session to current user
					((Sessions)getSession()).setUserid(account.getUserid());
					((Sessions)getSession()).setStudentid(studentid);
					
					//search for user description 
					User user = (User)sqlMap.queryForObject("User.getUserdesc", account.getUserid() );
					
					//set current user description
					((Sessions)getSession()).setDesc(user.getUserdesc(account.getUserid()));
					
					
					TreasurerHome page = new TreasurerHome();
					setResponsePage(page);
					
				}
				else if ((Integer.toString(account.getUserid())).equals("0"))
				{
					//set session to current user
					((Sessions)getSession()).setUserid(account.getUserid());
					((Sessions)getSession()).setStudentid(studentid);
					
					//search for user description 
					User user = (User)sqlMap.queryForObject("User.getUserdesc", account.getUserid() );
					
					//set current user description
					((Sessions)getSession()).setDesc(user.getUserdesc(account.getUserid()));
					
					
					AdministratorHome page = new AdministratorHome();
					setResponsePage(page);
					
				}
				
				else if ((Integer.toString(account.getUserid())).equals("4"))
				{
					//set session to current user
					((Sessions)getSession()).setUserid(account.getUserid());
					((Sessions)getSession()).setStudentid(studentid);
					
					//search for user description 
					User user = (User)sqlMap.queryForObject("User.getUserdesc", account.getUserid() );
					
					//set current user description
					((Sessions)getSession()).setDesc(user.getUserdesc(account.getUserid()));
					
					
					MemberHome page = new MemberHome();
					setResponsePage(page);
					
				}
				
			
			
			}
			else{
				  error("Invalid Username and Password");
				 }
		  }catch(SQLException e){
			  error("Invalid Username and Password. Try again.");
			  e.printStackTrace();
		  } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	 
		}
		
	};

add(new FeedbackPanel("error"));
add(new BookmarkablePageLink("home", HomePage.class));


form.add(id);
form.add(pass);

	
add(form);
}
}
